﻿using System.Globalization;
using System.Windows;
using System.Windows.Media;
using System.Windows.Shapes;

namespace ChromeTabs
{
    public class TabShape : Shape
    {
        public TabShape()
        {
            Stretch = Stretch.Fill;
        }

        public Path TabShapePath
        {
            get { return (Path)GetValue(TabShapePathProperty); }
            set { SetValue(TabShapePathProperty, value); }
        }

        // Using a DependencyProperty as the backing store for TabShapePath.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty TabShapePathProperty =
            DependencyProperty.Register("TabShapePath", typeof(Path), typeof(TabShape), new PropertyMetadata(null));



        protected override Size MeasureOverride(Size constraint)
        {
            if (constraint.Width == double.PositiveInfinity || constraint.Height == double.PositiveInfinity)
            {
                return Size.Empty;
            }
            // we will size ourselves to fit the available space
            return constraint;
        }
        protected override Geometry DefiningGeometry => GetGeometry();

        private Geometry GetGeometry()
        {
            if(TabShapePath!= null)
            {
                return TabShapePath.Data;
            }
            double width = DesiredSize.Width - StrokeThickness;



            //double height = 25;
            //double x1 = width ;
            //double x2 = width;
            //double x3 = width;
            //double x4 = width;
            //double x5 = width;



            //return Geometry.Parse(string.Format(CultureInfo.InvariantCulture, "M0,{5} C2.5,{5} 5,0 10,0 15,0 {0},0 {1},0 {2},0 {3},{5} {4},{5}", x1, x2, x3, x4, x5, height));


            //double width = 100; // 矩形宽度

 
            double height = 25; // 矩形高度
            double radius = 2.5; // 圆角半径
            double gap = 2; // 左右间隙

            // 调整后的宽度
            double adjustedWidth = width - 2 * gap;

            // 左下角开始点（向右移动2单位）
            double startX = gap;
            double startY = height;

            // 左上角点（向右移动2单位）
            double leftTopX = gap;
            double leftTopY = radius;

            // 右上角点（向左移动2单位）
            double rightTopX = adjustedWidth - radius;
            double rightTopY = 0;

            // 右下角点（向左移动2单位）
            double rightBottomX = adjustedWidth;
            double rightBottomY = height;

            // 路径字符串
            string pathData = string.Format(
                CultureInfo.InvariantCulture,
                "M{0},{1} " + // 左下角开始点
                "L{2},{3} " + // 左上角点
                "A{4},{4} 0 0 1 {5},{6} " + // 左上角圆弧
                "L{7},{8} " + // 右上角点
                "A{4},{4} 0 0 1 {9},{10} " + // 右上角圆弧
                "L{11},{12}", // 右下角点
                startX, startY,
                leftTopX, leftTopY,
                radius,
                gap + radius, 0,
                rightTopX, rightTopY,
                adjustedWidth, radius,
                rightBottomX, rightBottomY
            );

            // 创建几何路径
            var geometry = Geometry.Parse(pathData);

            return geometry;


        }
    }
}
